﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ControlePonto.Models;

namespace ControlePonto.Controllers
{
    public class FuncionarioController : Controller
    {
        PontoEntities ponto = new PontoEntities();
        //
        // GET: /Funcionario/

        public ActionResult Index()
        {            
            return View(ponto.Funcionario.ToList());
        }

        //
        // GET: /Funcionario/Details/5

        public ActionResult Details(int id)
        {
            var funcionario = ponto.Funcionario.Single(f => f.ID == id);
            return View(funcionario);
        }

        //
        // GET: /Funcionario/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Funcionario/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude="ID")] Funcionario f)
        {
            try
            {
                if (ModelState.IsValid)
                {

                    ponto.AddToFuncionario(f);
                    ponto.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Funcionario/Edit/5
 
        public ActionResult Edit(int id)
        {
            var funcionario = ponto.Funcionario.First(f => f.ID == id);
            return View(funcionario);
        }

        //
        // POST: /Funcionario/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            try
            {
                var original = ponto.Funcionario.First(f => f.ID == id);                
                if (!ModelState.IsValid)
                {
                    return View(original);
                }
                else
                {
                    UpdateModel(original);
                    ponto.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch ( Exception error)
            {
                ModelState.AddModelError("", error);                
                return View();
            }
        }

        //
        // GET: /Funcionario/Delete/5
 
        public ActionResult Delete(int id)
        {
            var original = ponto.Funcionario.First(f => f.ID == id);
            return View(original);
        }

        //
        // POST: /Funcionario/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var original = ponto.Funcionario.First(f => f.ID == id);
                ponto.DeleteObject(original);
                ponto.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
